/*
 * EdgePropertyDialog.java
 *
 * Created on March 22, 2007, 2:23 PM
 * Copyright 2007 Grotto Networking
 */

package EPDLEditor.UI.Dialogs;

import javax.swing.table.TableModel;

import EPDLEditor.MainEditor;
import EPDLEditor.Types.Context;
import EPDLEditor.Types.PartitionParameter;
import EPDLEditor.UI.PopulateListsUtils;

/**
 *
 * @author  Ronen Vaisenberg and Greg
 */
public class ContextPropertyDialog extends javax.swing.JDialog {
    Context context;
    /** Creates new form ContextPropertyDialog */
    public ContextPropertyDialog(java.awt.Frame parent, Context context) {
        super(parent, true);
        initComponents();
        this.context = context;
        setTitle("Context Definition");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jContextSubTypeComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jPartitionParamsTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jDoneButton = new javax.swing.JButton();
        jContextTypeComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jContextList = new javax.swing.JList();
        jCompOperator = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jContextIdTextPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edge Properties");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jContextSubTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jContextSubTypeComboBoxActionPerformed(evt);
            }
        });

        jLabel7.setText("Context Type:");

        jLabel3.setText("Composition:");

        jLabel6.setText("Context Dimension:");

        jPartitionParamsTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPartitionParamsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Parameter Name", "Parameter Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jPartitionParamsTable.setDragEnabled(true);
        jPartitionParamsTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane4.setViewportView(jPartitionParamsTable);

        jLabel5.setText("Context Identifier:");

        jLabel2.setText("Partition Parameters:");

        jDoneButton.setText("Done");
        jDoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDoneButtonokButtonHandler(evt);
            }
        });

        jContextTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Temporal", "Spatial", "State Oriented", "Segmentation Oriented", "Composite" }));
        jContextTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jContextTypeComboBoxActionPerformed(evt);
            }
        });
        jContextTypeComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jContextTypeComboBoxPropertyChange(evt);
            }
        });

        jLabel1.setText("Composition Contexts:");

        jLabel4.setText("Composition Operator");

        jContextList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Event 1", "Event 2", "Event 3", "Derived Event 1", "Derived Event 2", "Derived Event 3" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jContextList);

        jCompOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Intersection", "Union", "Difference" }));

        jScrollPane3.setViewportView(jContextIdTextPane);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 208, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(108, 108, 108))
                                    .add(layout.createSequentialGroup()
                                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 199, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jCompOperator, 0, 274, Short.MAX_VALUE)
                                    .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(12, 12, 12)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6)
                            .add(jLabel5)
                            .add(jLabel7)
                            .add(jLabel2))
                        .add(25, 25, 25)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                            .add(jContextSubTypeComboBox, 0, 429, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jContextTypeComboBox, 0, 429, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 413, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(8, 8, 8)))))
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(525, Short.MAX_VALUE)
                .add(jDoneButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 12, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jContextTypeComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED, 36, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLabel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jContextSubTypeComboBox))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 12, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 114, Short.MAX_VALUE)
                        .add(jLabel3))
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jCompOperator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jDoneButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jDoneButtonokButtonHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDoneButtonokButtonHandler
        store();
        setTitle(context.toString());
        MainEditor.vv.repaint();
        dispose();
    }//GEN-LAST:event_jDoneButtonokButtonHandler

    private void jContextTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jContextTypeComboBoxActionPerformed
    	jContextSubTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(Context.getSubTypeOptions(jContextTypeComboBox.getSelectedIndex())));
    	
    	boolean compositeContext = jContextTypeComboBox.getSelectedIndex()==Context.compositeContextId;
		this.jScrollPane4.setVisible(!compositeContext);
		this.jLabel2.setVisible(!compositeContext);
		
		this.jCompOperator.setVisible(compositeContext);
		this.jScrollPane1.setVisible(compositeContext);
		this.jLabel1.setVisible(compositeContext);
		this.jLabel3.setVisible(compositeContext);
		this.jLabel4.setVisible(compositeContext);
		
		// Next line is important as the selected index populates the parameter's table
    	jContextSubTypeComboBox.setSelectedIndex(0);
    }//GEN-LAST:event_jContextTypeComboBoxActionPerformed

    public void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        jContextList.setModel(new javax.swing.AbstractListModel() {             
            public int getSize() { return MainEditor.contexts.size(); }
            public Object getElementAt(int i) { return MainEditor.contexts.get(i).contextIdentifier; }
        });
        /**
         *
         * The line below is due to a bug in JAVA release, see
         * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4503845
         */
    	jPartitionParamsTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

    	populate(context);
    	
    }//GEN-LAST:event_formComponentShown

    private void jContextTypeComboBoxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jContextTypeComboBoxPropertyChange
    }//GEN-LAST:event_jContextTypeComboBoxPropertyChange

    private void jContextSubTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jContextSubTypeComboBoxActionPerformed
    	this.jPartitionParamsTable.setModel(Context.getTableModelForTypeAndSubType(jContextTypeComboBox.getSelectedIndex(),jContextSubTypeComboBox.getSelectedIndex()));
    }//GEN-LAST:event_jContextSubTypeComboBoxActionPerformed
    
    
    private void populate(Context context){  	
    	
       	//header
    	
    	this.jContextIdTextPane.setText(context.contextIdentifier);
    	this.jContextTypeComboBox.setSelectedItem(context.contextType);
    	this.jContextSubTypeComboBox.setSelectedItem(context.contextSubtype);
    	
    	
    	//payload
    	int partitionParamaters = context.partitionParameters==null?0:context.partitionParameters.length;	    	
    	TableModel tm = jPartitionParamsTable.getModel();
    	int i;
    	
    	for (i=0; i<partitionParamaters;i++){
    		tm.setValueAt(context.partitionParameters[i].name, i, 0);
    		tm.setValueAt(context.partitionParameters[i].value, i, 1);    		
    	}

    	//event 2 event relations
    	this.jCompOperator.setSelectedItem(context.compositionOperator);
    	this.jContextList.setSelectedIndices(PopulateListsUtils.getIDs4Strings(context.compositionContexts,MainEditor.contexts));
    	
    }
    private void store(){
    
       	//header
    	context.contextIdentifier=this.jContextIdTextPane.getText();
    	context.contextType = (String)this.jContextTypeComboBox.getSelectedItem();
    	context.contextSubtype = (String)this.jContextSubTypeComboBox.getSelectedItem();
    	
    	
    	//payload
    	int partitionParamaters = this.jPartitionParamsTable.getRowCount();
    	TableModel jm = this.jPartitionParamsTable.getModel();
    	int i;
    	for (i=0; i<partitionParamaters;i++){
    		if (((String)jm.getValueAt(i, 0)).compareTo("")==0)
    			break;
    	}
    	partitionParamaters = i;
    	context.partitionParameters = new PartitionParameter[partitionParamaters];
    	
    	for (i=0; i<partitionParamaters;i++){
    		context.partitionParameters[i] = new PartitionParameter();
    		context.partitionParameters[i].name = (String) jm.getValueAt(i, 0);
    		context.partitionParameters[i].value = (String) jm.getValueAt(i, 1);   	
    	}
    	//event 2 event relations
    	context.compositionOperator=(this.jContextTypeComboBox.getSelectedIndex()!=Context.compositeContextId)?null:(String)this.jCompOperator.getSelectedItem();
    	context.compositionContexts=(this.jContextTypeComboBox.getSelectedIndex()!=Context.compositeContextId)?null:PopulateListsUtils.getStrings4IDs(this.jContextList.getSelectedIndices(),MainEditor.contexts);
    	
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCompOperator;
    private javax.swing.JTextPane jContextIdTextPane;
    private javax.swing.JList jContextList;
    private javax.swing.JComboBox jContextSubTypeComboBox;
    private javax.swing.JComboBox jContextTypeComboBox;
    private javax.swing.JButton jDoneButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTable jPartitionParamsTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration//GEN-END:variables
    
}
